/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.leetCode;

import java.util.Stack;

/**
 * @author : sunzb(sunzb@hsit.com.cn)
 * @date: 2019/5/13
 */
// easy
public class _2_232_ImplementQueueUsingStacks {

    Stack<Integer> s1 = new Stack();
    Stack<Integer> s2 = new Stack();
    int front = 0;

    /** Initialize your data structure here. */
    public _2_232_ImplementQueueUsingStacks() {

    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        if (s1.empty())
            front = x;
        s1.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (s2.isEmpty()) {
            while (!s1.isEmpty()) {
                s2.push(s1.pop());
            }
        }
        return s2.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (!s2.isEmpty()) {
            front = s2.peek();
        }
        return front;
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return s2.isEmpty() && s1.isEmpty();
    }

    public static void main(String[] args) {
        _2_232_ImplementQueueUsingStacks queue = new _2_232_ImplementQueueUsingStacks();
        queue.push(-1);
        queue.push(1);
        System.out.println(queue.peek());
        queue.push(2);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
        System.out.println(queue.empty()); // returns false
    }
}
